﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using DAL;
using BLL.Util;

namespace BLL
{
    public class RecordBLL
    {
        /// <summary>
        /// 返回我最近记录的10笔交易记录
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<Record> GetMyRecordTop10(string accountId)
        {
            var items = CacheHelper.GetMyAllRecord(accountId);
            var foo = (from bar in items orderby bar.ID descending  select bar).Take(10);
            return foo;
        }
        /// <summary>
        /// 获取我全部的记录 fromDB
        /// </summary>
        /// <param name="accountId"></param>
        /// <returns></returns>
        internal static IEnumerable<Record> GetMyAllRecord(string accountId)
        {
            RecordDAL dal = new RecordDAL();
            return dal.QueryMyList(int.Parse(accountId));
        }
        /// <summary>
        /// 添加记录
        /// </summary>
        /// <param name="entity"></param>
        public static void AddRecord(Record entity)
        {
            RecordDAL dal = new RecordDAL();
            dal.Insert(entity);
            var card = CardBLL.GetMyCard(entity.AccountID.ToString(), entity.CardID.ToString());
            card.Money -= entity.Money;
            CardBLL.EditCard(card);
            var foo = CacheHelper.GetMyAllRecord(entity.AccountID.ToString());
            foo.Add(entity);
            
        }
        /// <summary>
        /// 用户自动记账
        /// </summary>
        /// <param name="account">用户账号</param>
        public static void AutoRecord(AutoSetting auto)
        {
            
            if (auto.LastTime.Date<=DateTime.Now.Date)
            {
                RecordDAL dal = new RecordDAL();
                dal.AutoInsert(auto);
                auto.LastTime = DateTime.Now.AddDays(1.0);
                AutoSettingBLL.UpdateAutoSetting(auto);
            }
        }
    }
}
